//config global variables
var PLAY_MODE = 0; //0: free mode and 1: real mode

var MatchState = {
	WAIT_MORE_PLAYERS: 0, 
	//wait forever! 				 
	//---PLAYER 2 JOIN--->	
	WAIT_PLAYERS_READY: 1,
	//wait 20 second. kick all user not ready!   
	//---DEAL--->
	BET: 2,
	//wait 20 second for cheking.
	//----TIMEOUT or ACCEPT ALL-->
	SHOW_RESULT: 3
	//wait 5 seconds.					              
	//QUIT--->
};

var MAX_PLAYERS = 4;
var CurrentRoomID = -1;

var GAME_ZONE = "MahjongZones";

var LOBBY_ROOM_NAME = "Mahjong Room";
var CURRPLAYERS = [];
var waitScene = null;
var myApp = null;
var config = {
    "host" : "192.168.15.41",//"widocom.no-ip.org", //""
    "port" : 8888,
    "debug" : false
};

var dataPlayers = [];
var isPlaying = false;

var dialog = null;
var conn = null;

var RoomInfo = {
    roomID: null,
    OwnerID: null
};

var players = {};

function isDifferent(data1, data2)
{
    if(data1.length != data2.length){
        return true;
    }

    for(var i = 0 ; i < data1.length ; i++){
        if(data1[i].id != data2[i].id){
            return true;
        }
    }

    return false;
}

//get url param
$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
        return null;
    }
    else{
        return results[1] || null;
    }
}

var language = "en";

window.onbeforeunload = function(e){
    if(isPlaying){
        return I18n["out room playing"];
    }
};

$(document).ready(function(){

    //for language
    if($.urlParam("language") == "jp"){
        I18n = I18n_jp;
        language = "jp";
    }

	window.scroll(0,1);
	
	dialog = new DialogManager();
	dialog.init();
	conn = new ConnectionManager(config, dialog);
	
	//load connect screen
	switchView(1);
	
	//set value for ui-loading-title
	$(".ui-loading-title").html("<span>" + I18n["connecting"] + "</span>");
	

	
	var sound = new SoundManager();
	sound.init();
	

	conn.connect(function(){		
		var user = new UserManager(conn, token);
		user.login(function(){
			switchView(2);
			var room = new RoomManager(conn, user, sound, dialog);
			room.init();
	
			$(".ui-mode-free").on("click touchend",function(){
				sound.clickEffect();
				switchView(3);
				room.setFreeMode();
			});
			
			$(".ui-mode-real").on("click touchend",function(){
				room.setRealMode();
				switchView(3);
				sound.clickEffect();
			});	
		}, function(event){
			//show login popup
		});
	}, function(errorMessage){
		
	});
});

function switchView(step){
	switch(step){
		case 1:
			//connect view
			$(".ui-loading-container").show();
			$(".ui-mode-container").hide();
			$(".ui-home-container").hide();
			$(".ui-gameplay-container").hide();
			var top = ($(window).height() - $(".ui-loading-title").height()) / 2;
			$(".ui-loading-title").css("margin-top", top);
			break;
		case 2:
			//choose mode view
			$(".ui-loading-container").hide();
			$(".ui-mode-container").show();
			$(".ui-home-container").hide();
			$(".ui-gameplay-container").hide();
			var top = ($(window).height() - $(".ui-mode-button-container").height()) / 2;
			$(".ui-mode-button-container").css("margin-top", top);
			break;
		case 3:
			//home view
			$(".ui-loading-container").hide();
			$(".ui-mode-container").hide();
			$(".ui-home-container").show();
			$(".ui-gameplay-container").hide();
			break;
		case 4:
			//play view
			$(".ui-loading-container").hide();
			$(".ui-mode-container").hide();
			$(".ui-home-container").hide();
			$(".ui-gameplay-container").show();
			break;
	}
}